package com.zhoulz.homework.chaptor02.node;

import io.netty.buffer.ByteBuf;

/**
 * 自定义数据包
 * @author zhoulz
 *
 */
public class Packet {
	// 数据包长度
	private int length;
	// 数据包类型
	private PacketType type;
	// 数据
	private ByteBuf data;
	
	public enum PacketType{
		TYPE((byte)0),
		/**
		 * 文件开始传输
		 */
		FILE_SEND_START((byte)1), 
		/**
		 * 文件传输中
		 */
		FILE_SEND_ING((byte)2), 
		/**
		 * 文件传输完成
		 */
		FILE_SEND_DONE((byte)3),
		/**
		 * 节点Shutdown
		 */
		NODE_SHUTDOWN((byte)4);
		
		private byte value;
		private PacketType(byte value) {
			this.value = value;
		}
		public byte getValue() {
			return value;
		}
		public void setValue(byte value) {
			this.value = value;
		}
		public PacketType getType(short t){
			switch (t) {
			case 1:
				return FILE_SEND_START;
			case 2:
				return FILE_SEND_ING;
			case 3:
				return FILE_SEND_DONE;
			case 4:
				return NODE_SHUTDOWN;
			default:
				return TYPE;
			}
		}
	}
	
	public Packet(int length, PacketType type, ByteBuf data) {
		super();
		this.length = length;
		this.type = type;
		this.data = data;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public PacketType getType() {
		return type;
	}
	public void setType(PacketType type) {
		this.type = type;
	}
	public ByteBuf getData() {
		return data;
	}
	public void setData(ByteBuf data) {
		this.data = data;
	}
	
	@Override
	public String toString() {
		return "Packet [length=" + length + ", type=" + type + ", data=" + data
				+ "]";
	}
	
}
